package com.ruoyi.generateTest.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.generateTest.domain.GeneratedExam;
import com.ruoyi.generateTest.service.IGeneratedExamService;
import com.ruoyi.generateTest.utils.GenerateQuestionsUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.generateTest.domain.GenerateTestPaper;
import com.ruoyi.generateTest.service.IGenerateTestPaperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.generateTest.utils.WordToExcelUtil.wordToExcelGeneral;

/**
 * 生成试题文件Controller
 *
 * @author 经纬线
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/generateTest/generate_test_paper")
public class GenerateTestPaperController extends BaseController
{
    @Autowired
    private IGenerateTestPaperService generateTestPaperService;

    /**
     * 查询生成试题文件列表
     */
    @PreAuthorize("@ss.hasPermi('list')")
    @GetMapping("/list")
    public TableDataInfo list(GenerateTestPaper generateTestPaper)
    {
        startPage();
        List<GenerateTestPaper> list = generateTestPaperService.selectGenerateTestPaperList(generateTestPaper);
        return getDataTable(list);
    }

    /**
     * 导出生成试题文件列表
     */
    @PreAuthorize("@ss.hasPermi('export')")
    @Log(title = "生成试题文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GenerateTestPaper generateTestPaper)
    {
        List<GenerateTestPaper> list = generateTestPaperService.selectGenerateTestPaperList(generateTestPaper);
        ExcelUtil<GenerateTestPaper> util = new ExcelUtil<GenerateTestPaper>(GenerateTestPaper.class);
        util.exportExcel(response, list, "生成试题文件数据");
    }

    /**
     * 获取生成试题文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(generateTestPaperService.selectGenerateTestPaperByFileId(fileId));
    }

    /**
     * 新增生成试题文件
     */
    @PreAuthorize("@ss.hasPermi('add')")
    @Log(title = "生成试题文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GenerateTestPaper generateTestPaper)
    {
        return toAjax(generateTestPaperService.insertGenerateTestPaper(generateTestPaper));
    }

    /**
     * 修改生成试题文件
     */
    @PreAuthorize("@ss.hasPermi('edit')")
    @Log(title = "生成试题文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GenerateTestPaper generateTestPaper)
    {
        return toAjax(generateTestPaperService.updateGenerateTestPaper(generateTestPaper));
    }

    /**
     * 删除生成试题文件
     */
    @PreAuthorize("@ss.hasPermi('remove')")
    @Log(title = "生成试题文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(generateTestPaperService.deleteGenerateTestPaperByFileIds(fileIds));
    }


    /**
     * word上传路径
     */
    private static String profile;
    @Value("${ruoyi.profile}")
    public void setFilePath(String path) {
        profile = path;
    }

    @Autowired
    IGeneratedExamService generatedExamService;
    /**
     * 用户上传文件生成指定题目数量和难度的试题
     * @param generate_test_form 生成试题表单数据
     * @return 生成成功结果
     * @throws Exception io异常
     */
    @Anonymous
    @ResponseBody
    @PostMapping(value = "/generate",produces = {"application/json;charset=utf-8"})
    public AjaxResult generate(@RequestBody Map<String,Object> generate_test_form) throws Exception {
        // 获取生成试题源文件路径
        GenerateTestPaper generateTestPaper = generateTestPaperService
                .selectGenerateTestPaperByFileId(((Integer) generate_test_form.get("generate_fileId"))
                        .longValue());
        File file = new File(profile+generateTestPaper
                .getFilePath()
                .replaceAll("^/profile",""));

        String FILEPATH = GenerateQuestionsUtil.SingleTalks(file,
                generate_test_form.get("name").toString(),
                Integer.parseInt(generate_test_form.get("single").toString()),
                Integer.parseInt(generate_test_form.get("multiple").toString()),
                Integer.parseInt(generate_test_form.get("torF").toString()),
                Integer.parseInt(generate_test_form.get("fillBlank").toString()),
                Integer.parseInt(generate_test_form.get("short").toString()),
                Integer.parseInt(generate_test_form.get("difficulty").toString()),
                1
        );
        // 生成试题数据插入数据库
        GeneratedExam generatedExam = new GeneratedExam();
        // 设置属性
        generatedExam.setFileId(((Integer) generate_test_form.get("generate_fileId")).longValue());
        generatedExam.setFileName(FILEPATH); // 使用生成的试题文件路径作为试题名称
        generatedExam.setSingleChoiceCount(((Integer) generate_test_form.get("single")).longValue());
        generatedExam.setMultipleChoiceCount(((Integer) generate_test_form.get("multiple")).longValue());
        generatedExam.setTorfCount(((Integer) generate_test_form.get("torF")).longValue());
        generatedExam.setFillBlankCount(((Integer) generate_test_form.get("fillBlank")).longValue());
        generatedExam.setShortAnswerCount(((Integer) generate_test_form.get("short")).longValue());
        generatedExam.setDifficulty(generate_test_form.get("difficulty").toString());
        // 保存
        generatedExamService.insertGeneratedExam(generatedExam);

        return AjaxResult.success("生成试题成功!");

    }


}
